package cn.gson.crm.control;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.gson.crm.model.mapper.zjProductDAO;
import cn.gson.crm.model.pojo.ConstactOrder;
import cn.gson.crm.model.pojo.OrderResult;
import cn.gson.crm.model.pojo.Orderdetails1;
import cn.gson.crm.model.pojo.Product;
import cn.gson.crm.model.vo.OrderDetailsVO;
import cn.gson.crm.service.OrderDetailsService;

@Controller
public class OrderDetailsControl {

	@Autowired
	private OrderDetailsService ods;
	@Autowired
	private zjProductDAO zdao;

	@GetMapping("/toOrderDetails")
	public String toOrderDetails() {
		return "zj/orderDetails";
	}
	
	@GetMapping("/toOdf")
	public String toOdf() {
		return "zj/orderDetailForm";
	}
	
	@GetMapping("/toOdf2")
	public String toOdf2() {
		return "zj/orderDetailForm2";
	}
	
	@GetMapping("deleteSomeDetails")
	@ResponseBody
	public OrderResult deleteSomeDetails(@RequestParam("array[]") Long row[]) {
		try {
			ods.deleteSome(row);
			return new OrderResult();
		} catch (Exception e) {
			e.printStackTrace();
			return new OrderResult(e);
		}
		
	}

	@GetMapping("/deleteOd")
	@ResponseBody
	public OrderResult deleteOd(Long odId) {
		try {
			ods.deleteOd(odId);
			return new OrderResult();
		} catch (Exception e) {
			e.printStackTrace();
			return new OrderResult(e);
		}
	}
	
	@GetMapping("/getAllOrderDetails")
	@ResponseBody
	public Map getAllOrderDetails(int page, int rows) {
		Map map = new HashMap<>();
		List<OrderDetailsVO> list = ods.getAllOrderDetails(page, rows);
		long count = ods.orderCount();
		map.put("total", count);
		map.put("rows", list);
		return map;
	}

	@PostMapping("/addNewOrderDetails")
	@ResponseBody
	public OrderResult addNewOrderDetails(Orderdetails1 con, Long cusIdvo, Long coIdvo, Long proId) {
		try {
			ods.addOrderDetails(con, cusIdvo, coIdvo, proId);
			return new OrderResult();
		} catch (Exception e) {
			e.printStackTrace();
			return new OrderResult(e);
		}
	}

	@PostMapping("/editOrderDetails")
	@ResponseBody
	public OrderResult editOrderDetails(Orderdetails1 con, Long odId,Long cusIdvo, Long coIdvo, Long proId) {
		try {
			ods.addOrderDetails(con, cusIdvo, coIdvo, proId);
			return new OrderResult();
		} catch (Exception e) {
			e.printStackTrace();
			return new OrderResult(e);
		}
	}
	
	@GetMapping("/getodById")
	@ResponseBody
	public OrderDetailsVO getodById(Long odId) {
		return ods.getodById(odId);
	}
	
	@RequestMapping("getOrdersById")
	@ResponseBody
	public List<ConstactOrder> getOrdersById(Integer coId){
		return ods.getByOrderId(coId);
		
	}
	
	@RequestMapping("getProductByOid")
	@ResponseBody
	public List<Product> getProductByOid(Long oid){
		return ods.findProByOrderId(oid);
	}
	
	@RequestMapping("getAllProduct")
	@ResponseBody
	public List<Product> getAllProduct(){
		return zdao.findAllProduct();
	}
	
	@RequestMapping("getProductByOi")
	@ResponseBody
	public List<Product> getProductByOi(Long oidd){
		System.out.println(oidd);
		return ods.findProByOrder(oidd);
	}
}
